package com.TaskOrganizer.dialogs;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import com.TaskOrganizer.core.AppCore;
import com.TaskOrganizer.gui.TaskButton;
import com.TaskOrganizer.managers.TaskPriority;
import com.TaskOrganizer.task.Task;

public class ListTasksDialog extends JFrame {

	private static final long serialVersionUID = -5980497835108216702L;
	private JButton cancel, update;
	private JPanel tasksButtonPanel;
	private JScrollPane tasksScrollPane;
	private ButtonGroup radioButtons;
	private JRadioButton resolved, unresolved;

	public ListTasksDialog() {
		super("Tasks");
		if (AppCore.getInstance().getTaskList().size() < 1) {
			JOptionPane.showMessageDialog(null, "No tasks to be listed!");
			this.dispose();
			return;
		}
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setVisible(true);
		Toolkit kit = Toolkit.getDefaultToolkit();
		URL url = getClass().getResource("/com/TaskOrganizer/icons/icon.png");
		Image img = kit.createImage(url);
		setIconImage(img);
		setLayout(new GridBagLayout());

		getBottomPanel();
		getTasksPanel();

		pack();
		Dimension dim = kit.getScreenSize();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
		setResizable(false);
	}

	private void getBottomPanel() {
		if (cancel == null)
			cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		if (update == null) {
			update = new JButton("Update");
			update.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					remove(tasksScrollPane);
					getTasksPanel();
					revalidate();
				}
			});
		}
		JPanel pan = new JPanel(new GridLayout(2, 2));
		radioButtons = new ButtonGroup();
		resolved = new JRadioButton("resolved");
		unresolved = new JRadioButton("unresolved");
		unresolved.setSelected(true);
		radioButtons.add(resolved);
		radioButtons.add(unresolved);

		pan.add(resolved);
		pan.add(unresolved);
		pan.add(update);
		pan.add(cancel);
		add(pan);
	}

	private void getTasksPanel() {
		tasksScrollPane = new JScrollPane(getTasksButtonPanel());
		tasksScrollPane.setPreferredSize(new Dimension(300, 70));
		tasksScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		add(tasksScrollPane);
	}

	private JPanel getTasksButtonPanel() {
		if (unresolved.isSelected()) {
			System.out.println(1);
			tasksButtonPanel = new JPanel(new GridLayout(AppCore.getInstance()
					.getUnesolvedCount(), 1));
			ArrayList<Task> tmp = AppCore.getInstance().getTaskList();
			for (Task task : tmp) {
				if (!task.isResolved()
						&& task.getPriority().equals(TaskPriority.HIGH)) {
					TaskButton btn = new TaskButton(task, this);
					tasksButtonPanel.add(btn);
				}
			}
			for (Task task : tmp) {
				if (!task.isResolved()
						&& task.getPriority().equals(TaskPriority.NORMAL)) {
					TaskButton btn = new TaskButton(task, this);
					tasksButtonPanel.add(btn);
				}
			}

		} else {
			System.out.println(2);
			tasksButtonPanel = new JPanel(new GridLayout(AppCore.getInstance()
					.getTaskList().size()
					- AppCore.getInstance().getUnesolvedCount(), 1));
			ArrayList<Task> tmp = AppCore.getInstance().getTaskList();
			for (Task task : tmp) {
				if (task.isResolved()) {
					TaskButton btn = new TaskButton(task, this);
					tasksButtonPanel.add(btn);
				}
			}
		}
		return tasksButtonPanel;
	}

}
